var ul = document.getElementById('list');
var addbtn = document.getElementById('addbtn');
var content = document.getElementById('content');
var delbtn = document.getElementById('delbtn');
var listNode = document.getElementById('listNode');
var list = [
    // {
    //     content: 'ganjinxie'
    // },
    // {
    //     content: 'niuaniuaniua'
    // }
];

var tpl = '<li><p id = "listNode">{%content%}</p ><input type="button" value="修改" class="commonbtn"><input type="button" value="删除" class="commonbtn" id = "delbtn"></li>';

var fillList = function () {
    if (list.length == 0) {
        ul.innerHTML = '<li>没有内容</li>';
        return;
    }
    var str = '';
    for (var i = 0; i < list.length; i++) {
        str += tpl.replace('{%content%}', list[i].content);
    }
    ul.innerHTML = str;
}

addbtn.addEventListener('click', function() {
    // 先获取输入的数据
    if (content.value == '') {
        alert('请输入内容');
        return;
    }
    var obj = {
        content : content.value,
    }
    list.push(obj);
    content.value = '';
    saveList();
    fillList();
});

var saveList = function() {
    // 保存到本地
    localStorage.setItem('listData', JSON.stringify(list));
};
var getList = function() {
    var localList = localStorage.getItem('listData');
    localList = JSON.parse(localList);
    return localList;
}
var main = function() {
    if (localStorage.getItem('listData')) {
        list = getList();
    } else {
        list = [];
    }
    fillList();
}
main();

// 删除数据
if (list.length != 0) {
//     delbtn.addEventListener('click', function(){
//         console.log('求求了');
//     });
// };
    delbtn.onclick = function () {
        alert('肿么会这样');
    }
}